package org.hxyjs.dao.impl;

import org.hxyjs.dao.BooksDao;
import org.hxyjs.entity.Books;
import org.hxyjs.entity.Books;
import org.hxyjs.mapper.BooksMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("booksDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class BooksDaoImpl implements BooksDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private BooksMapper booksMapper;
    public List<Books>  getAllBookss() throws Exception{
        return booksMapper.selectByExample(null);
    }

    @Override
    public Integer deleteBooksByBno(String bno)  throws Exception{
        return booksMapper.deleteByPrimaryKey(bno);
    }

    @Override
    public Integer updateBooksByBno(Books books)  throws Exception{
        return booksMapper.updateByPrimaryKey(books);
    }

    @Override
    public Integer addBooks(Books books)  throws Exception{
        books.setBno(initId());
        return booksMapper.insertSelective(books);
    }
    public String initId(){
        String maxBno = booksMapper.getMaxBno();
        System.out.println(GenNumTools.initId("",3,maxBno));
        return GenNumTools.initId("",3,maxBno);
    }
    @Override
    public List<Books> getBooksByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)booksMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return booksMapper.getBooksByPage(map);
    }
    @Override
    public String getMaxBno(){
        return booksMapper.getMaxBno();
    }
}
